package xin.printk.android.view.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;

import java.util.List;

public abstract class AbstractRecyclerAdapter<T> extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    public static final int ITEM_TYPE_HEADER = 0;
    public static final int ITEM_TYPE_FOOTER = 1;
    public static final int ITEM_TYPE_CONTENT = 2;

    private View mHeaderView;
    private View mFooterView;

    protected Context mContext;
    protected List<T> mData;

    public AbstractRecyclerAdapter(Context context, List<T> list) {
        mContext = context;
        mData = list;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder holder = null;
        if (viewType == ITEM_TYPE_HEADER) {
            if (mHeaderView != null) {
                holder = new HeaderViewHolder(mHeaderView);
            }
        } else if (viewType == ITEM_TYPE_FOOTER) {
            if (mFooterView != null) {
                holder = new FooterViewHolder(mFooterView);
            }
        } else {
            holder = createContentHolder(parent);
        }
        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        if (holder instanceof HeaderViewHolder) {

        } else if (holder instanceof FooterViewHolder) {

        } else {
            bindContentHolder(holder, mHeaderView != null ? position - 1 : position);
        }
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mListener != null) {
                    mListener.onClick(v, position);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        int dataSize = mData == null ? 0 : mData.size();
        if (mHeaderView != null && mFooterView != null) {
            return dataSize + 2;
        } else if (mHeaderView != null) {
            return dataSize + 1;
        } else if (mFooterView != null) {
            return dataSize + 1;
        }
        return dataSize;
    }

    @Override
    public int getItemViewType(int position) {
        if (mHeaderView == null && mFooterView == null) {
            return ITEM_TYPE_CONTENT;
        }
        if (mHeaderView != null && position == 0) {
            return ITEM_TYPE_HEADER;
        }
        if (mFooterView != null && position == getItemCount() - 1) {
            return ITEM_TYPE_FOOTER;
        }
        return ITEM_TYPE_CONTENT;
    }

    public void setHeaderView(View view) {
        if (mHeaderView == null) {
            mHeaderView = view;
            notifyItemInserted(0);
        }
    }

    public void removeHeaderView() {
        if (mHeaderView != null) {
            notifyItemRemoved(0);
            mHeaderView = null;
        }
    }

    public void setFooterView(View view) {
        if (mFooterView == null) {
            mFooterView = view;
            notifyItemInserted(getItemCount() - 1);
        }
    }

    public void removeFooterView() {
        if (mFooterView != null) {
            notifyItemRemoved(getItemCount() - 1);
            mFooterView = null;
        }
    }

    public abstract RecyclerView.ViewHolder createContentHolder(ViewGroup parent);

    public abstract void bindContentHolder(RecyclerView.ViewHolder holder, int position);

    static class HeaderViewHolder extends RecyclerView.ViewHolder {

        HeaderViewHolder(View itemView) {
            super(itemView);
        }
    }

    static class FooterViewHolder extends RecyclerView.ViewHolder {

        FooterViewHolder(View itemView) {
            super(itemView);
        }
    }

    private OnItemClickListener mListener;

    public interface OnItemClickListener {

        void onClick(View view, int position);

    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        mListener = listener;
    }

}
